package com.apanal.qlife.sys.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.apanal.qlife.common.annotation.CurrentUser;
import com.apanal.qlife.common.annotation.LogType;
import com.apanal.qlife.common.annotation.SystemLog;
import com.apanal.qlife.common.constants.Constants;
import com.apanal.qlife.common.dictionary.DictionaryMap;
import com.apanal.qlife.sys.model.Resource;
import com.apanal.qlife.sys.model.User;
import com.apanal.qlife.sys.service.ResourceService;

/**
 * 资源控制类
 * 
 * 
 * @author shuliangxing
 * 
 * @date 2015-7-20下午5:51:42
 */
@Controller
@RequestMapping("/resource")
public class ResourceController {

	@Autowired
	private ResourceService service;

	/**
	 * 查询所有
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "资源管理", operation = "查询资源树", type = LogType.SELECT)
	@RequiresPermissions("sys:resource:view")
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model model) {
		model.addAttribute("list", service.selectAll());
		return "sys/resourceList";
	}

	/**
	 * 转向新增页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequiresPermissions("sys:resource:create")
	@RequestMapping(value = "/{id}/create", method = RequestMethod.GET)
	public String toCreate(@PathVariable("id") Long id, Model model) {
		// 设置公共数据
		setCommonData(model);

		Resource parent = service.selectById(id);
		model.addAttribute("parent", parent);
		Resource child = new Resource();
		child.setResPId(id);
		child.setResPIds(parent.makeSelfAsParentIds());
		model.addAttribute("resource", child);
		// 操作标识
		model.addAttribute("op", "新增资源");
		return "sys/resourceEdit";
	}

	/**
	 * 新增
	 * 
	 * @param user
	 * @param resource
	 * @param redirectAttributes
	 * @return
	 */
	@SystemLog(module = "资源管理", operation = "新增资源", type = LogType.CREATE)
	@RequiresPermissions("sys:resource:create")
	@ResponseBody
	@RequestMapping(value = "/{resId}/create", method = RequestMethod.POST)
	public String create(@CurrentUser User user, Resource resource) {
		resource.setInsertBy(user.getUseId());
		return service.insert(resource);
	}

	/**
	 * 转向更新页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequiresPermissions("sys:resource:update")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String toUpdate(@PathVariable("id") Long id, Model model) {
		// 设置公共数据
		setCommonData(model);

		model.addAttribute("resource", service.selectById(id));
		// 操作标识
		model.addAttribute("op", "修改资源");
		return "sys/resourceEdit";
	}

	/**
	 * 更新
	 * 
	 * @param user
	 * @param resource
	 * @param redirectAttributes
	 * @return
	 */
	@SystemLog(module = "资源管理", operation = "修改资源", type = LogType.UPDATE)
	@RequiresPermissions("sys:resource:update")
	@ResponseBody
	@RequestMapping(value = "/{resId}/update", method = RequestMethod.POST)
	public String update(@CurrentUser User user, Resource resource) {
		resource.setLastUpdateBy(user.getUseId());
		return service.updateByIdSelective(resource);
	}

	/**
	 * 转向详情页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequiresPermissions("sys:resource:view")
	@RequestMapping(value = "/{id}/detail", method = RequestMethod.GET)
	public String toDetail(@PathVariable("id") Long id, Model model) {
		model.addAttribute("resource", service.selectById(id));
		model.addAttribute("op", "查看资源");
		return "sys/resourceDetail";
	}

	/**
	 * 硬删除,包括子资源
	 * 
	 * @param user
	 * @param id
	 * @param redirectAttributes
	 * @return
	 */
	@SystemLog(module = "资源管理", operation = "删除资源", type = LogType.DELETE)
	@RequiresPermissions("sys:resource:delete")
	@ResponseBody
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String delete(@PathVariable("id") Long id) {
		return service.deleteById(id);
	}

	/**
	 * 检查资源名是否存在,修改时排除当前资源名称
	 * 
	 * @param resId
	 *            资源id,新增时传0
	 * @param resName
	 *            资源名称
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/{resId}/{resName}/checkResourceNameExist", method = RequestMethod.GET)
	public Boolean checkResourceNameExist(@PathVariable("resId") Long resId,
			@PathVariable("resName") String resName) {
		return service.checkResourceNameExist(resId, resName);
	}

	/**
	 * 启用/停用
	 * 
	 * @param user
	 * @param resource
	 * @return
	 */
	@SystemLog(module = "资源管理", operation = "启用/停用资源", type = LogType.UPDATE)
	@RequiresPermissions("sys:resource:start/stop")
	@ResponseBody
	@RequestMapping(value = "/{resId}/{normal}/updateToEnableOrDisable", method = RequestMethod.GET)
	public String updateToEnableOrDisable(@CurrentUser User user,
			Resource resource) {
		resource.setLastUpdateBy(user.getUseId());
		return service.updateToEnableOrDisable(resource);
	}

	/**
	 * 设置公共数据
	 * 
	 * @param model
	 */
	private void setCommonData(Model model) {
		// 资源类型
		model.addAttribute("resourceType",
				DictionaryMap.getList(Constants.RESOURCE_TYPE));
	}

}
